package com.tbit.uqbike.center.provide.impl;

import com.tbit.uqbike.center.constant.SystemData;
import com.tbit.uqbike.center.pojo.MachineStatus;
import com.tbit.uqbike.center.provide.MachineStatusProvideService;
import com.tbit.uqbike.center.service.BatteryModelDetailService;
import com.tbit.uqbike.center.service.MachineStatusService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class MachineStatusProvideServiceImpl implements MachineStatusProvideService {
    @Autowired
    private BatteryModelDetailService batteryModelDetailService;
    @Autowired
    private MachineStatusService machineStatusService;

    @Override
    public List<MachineStatus> getByMachineIds(List<Integer> machineIds) {
        List<MachineStatus> machineStatuses = new ArrayList<MachineStatus>();

        for (Integer machineId : machineIds) {
            MachineStatus machineStatus = SystemData.machineStatusMap.get(machineId);

            /**加载电量信息*/
            if (machineStatus == null) {
                machineStatusService.loadMachineStatus(machineId);
                machineStatus = SystemData.machineStatusMap.get(machineId);
            }

            if (machineStatus != null) {
                /**更新电量信息*/
                if (machineStatus.getSocPercent() == -1 || machineStatus.getSurplusMileage() == -1) {
                    batteryModelDetailService.updateMachineBattery(machineId);
                }
                /**如果没有位置，加载位置*/
                if (machineStatus.getPosDt() == null) {
                    machineStatusService.loadPosition(machineId);
                }

                machineStatuses.add(SystemData.machineStatusMap.get(machineId));
            }
        }

        return machineStatuses;
    }


}  